package labox.innovation.gameserver.skills.l2skills;

import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.Inventory;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.InventoryUpdate;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.StatsSet;
import labox.innovation.gameserver.templates.item.L2Weapon;

/**
 * 
 * @author nBd
 */
public class L2SkillChangeWeapon extends L2Skill {

	/**
	 * @param set
	 */
	public L2SkillChangeWeapon(StatsSet set) {
		super(set);
	}

	/**
	 * @see labox.innovation.gameserver.model.L2Skill#useSkill(labox.innovation.gameserver.model.actor.L2Character,
	 *      labox.innovation.gameserver.model.L2Object[])
	 */
	@Override
	public void useSkill(L2Character caster, L2Object[] targets) {
		if (caster.isDead())
			return;

		if (!(caster instanceof FuzePcInstance))
			return;

		FuzePcInstance player = (FuzePcInstance) caster;

		L2Weapon weaponItem = player.getActiveWeaponItem();

		if (weaponItem == null)
			return;

		L2ItemInstance wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_HAND);
		if (wpn == null)
			wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_HAND);

		if (wpn != null) {
			if (wpn.isWear())
				return;

			int newItemId = 0;
			int enchantLevel = 0;

			if (weaponItem.getChangeWeaponId() != 0) {
				newItemId = weaponItem.getChangeWeaponId();
				enchantLevel = wpn.getEnchantLevel();

				if (newItemId == -1)
					return;

				L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(wpn.getItem().getBodyPart());
				InventoryUpdate iu = new InventoryUpdate();
				for (L2ItemInstance item : unequiped)
					iu.addModifiedItem(item);

				player.sendPacket(iu);

				if (unequiped.length > 0) {
					byte count = 0;

					for (L2ItemInstance item : unequiped) {
						if (!(item.getItem() instanceof L2Weapon)) {
							count++;
							continue;
						}

						SystemMessage sm = null;
						if (item.getEnchantLevel() > 0) {
							sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
							sm.addNumber(item.getEnchantLevel());
							sm.addItemName(item);
						} else {
							sm = new SystemMessage(SystemMessageId.S1_DISARMED);
							sm.addItemName(item);
						}
						player.sendPacket(sm);
					}

					if (count == unequiped.length)
						return;
				} else {
					return;
				}

				L2ItemInstance destroyItem = player.getInventory().destroyItem("ChangeWeapon", wpn, player, null);

				if (destroyItem == null)
					return;

				L2ItemInstance newItem = player.getInventory().addItem("ChangeWeapon", newItemId, 1, player, destroyItem);

				if (newItem == null)
					return;

				newItem.setEnchantLevel(enchantLevel);
				player.getInventory().equipItem(newItem);

				SystemMessage msg = null;

				if (newItem.getEnchantLevel() > 0) {
					msg = new SystemMessage(SystemMessageId.S1_S2_EQUIPPED);
					msg.addNumber(newItem.getEnchantLevel());
					msg.addItemName(newItem);
				} else {
					msg = new SystemMessage(SystemMessageId.S1_EQUIPPED);
					msg.addItemName(newItem);
				}
				player.sendPacket(msg);

				InventoryUpdate u = new InventoryUpdate();
				u.addRemovedItem(destroyItem);
				u.addItem(newItem);
				player.sendPacket(u);

				player.broadcastUserInfo();
			}

		}
	}
}
